﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;
using System.Threading;
using System.Diagnostics;

namespace CodeSample
{
    /// <summary>
    /// Interaction logic for AboutBox.xaml
    /// </summary>
    public partial class AboutBox : Window
    {
        private volatile static AboutBox instance = null;

        private AboutBox()
        {
            InitializeComponent();

            this.txtVersion.Text += " " + this.AssemblyVersion;
            this.txtCopyright.Text = this.AssemblyCopyright;
            this.txtDescription.Text = this.AssemblyDescription;
        }

        public static AboutBox GetInstance()
        {
            if (instance == null)
            {
                Mutex mutex = new Mutex();
                mutex.WaitOne();

                if (instance == null)
                    instance = new AboutBox();

                mutex.Close();
            }
            return instance;
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            instance = null;
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        private void txtContact_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Process.Start("mailto:HiepHN60508@fpt.edu.vn?subject=Code%20Sample%20v" + this.AssemblyVersion + ": ");
        }
    }
}
